package com.example.yrdev.ordering_system.Report;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.yrdev.ordering_system.Adapter.GeneraLReportAdapter;
import com.example.yrdev.ordering_system.Common.CommonActivity;
import com.example.yrdev.ordering_system.Database.DingHanDB;
import com.example.yrdev.ordering_system.Database.PurchasedItemTB;
import com.example.yrdev.ordering_system.Model.CardRecord;
import com.example.yrdev.ordering_system.Model.PurchaseReport;
import com.example.yrdev.ordering_system.R;
import com.example.yrdev.ordering_system.Utils.GlobalFunction;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by luke on 10/14/16.
 */

public class GeneralReportActivity extends CommonActivity {
@InjectView(R.id.container_List)ExpandableListView containerList;

@InjectView(R.id.totalAmount)TextView totalAmount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        showBackButton();
        setContentView(R.layout.general_report_layout);
        ButterKnife.inject(this);
        setBarTitle("总报表");
        initView();

    }
    GeneraLReportAdapter adapter;
    @Override
    public void initView() {
        super.initView();
    String[] titleArr=new String[]{"随机/定而消费","取消扣款","商品消费(现金)","商品消费退款(现金)"
    ,"商品消费(刷卡)","商品消费退款(刷卡)"};
         adapter=new GeneraLReportAdapter(this,titleArr);
            containerList.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                @Override
                public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
                  String val="";

                    if (adapter.totalIncome>0 ){

                        val="+ "+String.valueOf(adapter.totalIncome);
                        totalAmount.setTextColor(ContextCompat.getColor(GeneralReportActivity.this,R.color.green));

                    }else if (adapter.totalIncome==0){

                        val=String.valueOf(adapter.totalIncome);


                    }else {
                        val="- "+Math.abs(adapter.totalIncome);

                        totalAmount.setTextColor(ContextCompat.getColor(GeneralReportActivity.this,R.color.redColor));
                    }

                    val=val+"元";

                    totalAmount.setText(val);

            containerList.removeOnLayoutChangeListener(this);
                }
            });

        containerList.setAdapter(adapter);

    }

    public  Double calFixRandTotalPurchase(ArrayList<CardRecord>recordArrayList){
        Double amount=0.;

        for ( CardRecord cardRecord:recordArrayList){

            amount=amount+cardRecord.getPurchaseAmount();

        }

        return amount;

    }

    public HashMap<String,Object> commodityTotalPurchase(){
        double amount=0.0;
        HashMap hashMap=new HashMap();

        ArrayList<PurchaseReport> reports=PurchaseReport.getPurchaseReport(this);
        for (PurchaseReport purchaseReport: reports){

            amount=amount+Double.valueOf(purchaseReport.getSaleCost());

        }
            hashMap.put("price",String.valueOf(amount));
            hashMap.put("count",String.valueOf(reports.size()));

        return hashMap;


    }





}
